package com.free.mini.core.serve.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.free.mini.common.base.ResultCode;
import com.free.mini.core.serve.entity.StudyModule;
import com.free.mini.core.serve.exception.CoreServiceException;
import com.free.mini.core.serve.mapper.StudyModuleMapper;
import com.free.mini.core.serve.service.IStudyModuleService;
import com.free.mini.core.serve.utils.IDGenerate;
import com.free.mini.core.server.vo.req.StudyModuleCreateReqVO;
import com.free.mini.core.server.vo.req.StudyModuleQueryReqVO;
import com.free.mini.core.server.vo.req.StudyModuleUpdateReqVO;
import com.free.mini.core.server.vo.resp.StudyModuleQueryRespVO;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;

/**
 * 学习模块表 Service实现
 *
 * @author dongsufeng
 * @date 2023-08-02 17:50:20
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class StudyModuleServiceImpl extends ServiceImpl<StudyModuleMapper, StudyModule> implements IStudyModuleService {

    private final StudyModuleMapper studyModuleMapper;

    @Override
    public List<StudyModuleQueryRespVO> findStudyModules(StudyModuleQueryReqVO reqVO) {
        StudyModule studyModule = BeanUtil.copyProperties(reqVO, StudyModule.class);
        LambdaQueryWrapper<StudyModule> queryWrapper = new LambdaQueryWrapper<>(studyModule);
        List<StudyModule> studyModules = this.baseMapper.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(studyModules)){
           return studyModules.stream().map(map ->
               BeanUtil.copyProperties(map, StudyModuleQueryRespVO.class)
            ).collect(Collectors.toList());
        }
        return null;
    }

    @Override
    public Page<StudyModuleQueryRespVO> page(StudyModuleQueryReqVO reqVO) {
        StudyModule studyModule = BeanUtil.copyProperties(reqVO, StudyModule.class);
        LambdaQueryWrapper<StudyModule> queryWrapper = new LambdaQueryWrapper<>(studyModule);
        queryWrapper.eq(StringUtils.isNotBlank(reqVO.getModuleCode()), StudyModule::getModuleCode,reqVO.getModuleCode());
        queryWrapper.eq(StudyModule::getDelFlag,0);
        Page<StudyModule> page = new Page<>(reqVO.getPageIndex(),reqVO.getPageSize());
        IPage<StudyModule> studyQuestionPage = this.page(page, queryWrapper);
        List<StudyModuleQueryRespVO> respVOList = studyQuestionPage.getRecords().stream().map(map ->
                BeanUtil.copyProperties(map, StudyModuleQueryRespVO.class)
        ).collect(Collectors.toList());
        Page<StudyModuleQueryRespVO> respVOPage = new Page<>();
        respVOPage.setRecords(respVOList);
        respVOPage.setCurrent(studyQuestionPage.getCurrent());
        respVOPage.setTotal(studyQuestionPage.getTotal());
        return respVOPage;
    }

    @Override
    public StudyModuleQueryRespVO detail(StudyModuleQueryReqVO reqVO) {
        LambdaQueryWrapper<StudyModule> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StudyModule::getModuleCode,reqVO.getModuleCode());
        queryWrapper.last("limit 1");
        StudyModule one = this.getOne(queryWrapper);
        if (ObjectUtil.isEmpty(one)){
            return null;
        }
        return BeanUtil.copyProperties(one,StudyModuleQueryRespVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean createStudyModule(StudyModuleCreateReqVO reqVO) {
        synchronized (reqVO.getModuleName()) {
            LambdaQueryWrapper<StudyModule> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(StudyModule::getModuleName,reqVO.getModuleName().trim());
            wrapper.last("limit 1");
            StudyModule studyModule = this.getOne(wrapper);
            if (ObjectUtil.isNotEmpty(studyModule)){
                throw new CoreServiceException(ResultCode.PARAM_ERROR.getCode(),"模块名称已存在");
            }
            studyModule = BeanUtil.copyProperties(reqVO, StudyModule.class);
            studyModule.setModuleCode(IDGenerate.idGenerateStr());
            studyModule.setCreateUser("");
            return this.save(studyModule);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateStudyModule(StudyModuleUpdateReqVO reqVO) {
        LambdaUpdateWrapper<StudyModule> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(StudyModule::getModuleCode,reqVO.getModuleCode());
        return this.update(BeanUtil.copyProperties(reqVO,StudyModule.class),wrapper);
    }

}
